var baseData = [
    { name: '精灵云', id: '0' },
    { name: '研发部', id: '0-0', parentkey: '0' },
    { name: '财务部', id: '0-1', parentkey: '0' },
    { name: '质量部', id: '0-2', parentkey: '0' },
    { name: '边缘云业务线', id: '0-0-0', parentkey: '0-0' },
    { name: '大数据业务线', id: '0-0-1', parentkey: '0-0' },
    { name: 'devloops业务线', id: '0-0-2', parentkey: '0-0' },
    { name: '仿真业务线', id: '0-0-3', parentkey: '0-0' },
    { name: '审计部', id: '0-1-0', parentkey: '0-1' },
    { name: '会计部', id: '0-1-1', parentkey: '0-1' },
    { name: '测试部', id: '0-2-0', parentkey: '0-2' },
    { name: '运维部', id: '0-2-1', parentkey: '0-2' },
];
var deepClone = function (target) {
    var result;
    if (typeof target !== 'object')
        return target;
    if (Array.isArray(target)) {
        result = [];
        target.forEach(function (item) {
            result.push(deepClone(item));
        });
    }
    else {
        result = {};
        for (var i in target) {
            result[i] = deepClone(target[i]);
        }
    }
    return result;
};
var cloneData = deepClone(baseData);
// 将对象数组转换成一棵树
var treeList = [];
var transferTree = function (list) {
    list.forEach(function (item, i) {
        item.children = [];
        if (!item.parentkey) {
            treeList.push(item);
        }
        list.forEach(function (item1, j) {
            if (item.id === item1.parentkey) {
                item.children.push(item1);
            }
        });
    });
    return treeList;
};
treeList = transferTree(cloneData);
console.log(treeList);
// 将树扁平化
var treeFlatArr = [];
var flatTree = function (tree) {
    tree.forEach(function (item, i) {
        if (!item.parentkey) {
            treeFlatArr.push(item);
            flatTree(item.children);
        }
        if (item.parentkey) {
            treeFlatArr.push(item);
            if (item.children.length !== 0) {
                flatTree(item.children);
            }
        }
    });
    return treeFlatArr;
};
treeFlatArr = flatTree(treeList);
console.log(treeFlatArr);
// 获取id数据
var idArr = [];
var getId = function (list, id) {
    idArr.push(id);
    list.forEach(function (item, i) {
        if (id === item.id) {
            if (item.parentkey) {
                getId(list, item.parentkey);
            }
        }
    });
    return idArr;
};
// 获取传入数组型的ids
var totalArr = [];
var getIdArr = function (list, ids) {
    ids.forEach(function (item, i) {
        idArr = [];
        totalArr.push(getId(list, item));
    });
    console.log(totalArr);
};
getIdArr(treeFlatArr, ['0-2-1', '0-2', '0-0-1']);
